package nz.ac.massey.doi_information_extractor.data;

import java.text.ParseException;

import javax.swing.text.DefaultFormatter;

@SuppressWarnings("serial")
public class PortNumberFormatter extends DefaultFormatter{

    public Object stringToValue(String text) throws ParseException {

    	if(text.length() == 0)
    		return text;

    	char[] chars = text.toCharArray();

    	for(int index = 0; index < chars.length; index++){
    		if(!Character.isDigit(chars[index]))
    			throw new ParseException("Pattern did not match", 0);
    	}

    	int value = Integer.parseInt(text);

    	if(value < 0 || value > 65535)
    		throw new ParseException("Pattern did not match", 0);

        return text;
    }
}
